#!/bin/bash
#THIS SCRIPT IS AN INSTALL SCRIPT TO INSTALL CARBLOGGER ONTO
#UBUNTU 10.04 or later, Linux Mint 10, or later.

clear
if [[ `id -u` -ne 0 ]]; then
{
	echo "'$0' Needs to be run as root."
	echo "Please ^c out and sudo su before running"
	exit;
}
else
{
	clear
	if !(nslookup google.com); then
		{
			echo "Sorry this script needs network to continue. Exiting";
			exit;
		}
	fi

	clear

	#echo 'Enter the name of the install log. I will write it to the desktop of the default admin user:'
	#echo "";
	install_log="install.txt."	
	#read install_log
	#wait $!
	#clear
	echo 'Enter the exact name of the default, administrative user [e.g., aqdms or whomever it was you configured in the last screen ]:'
	read start
	wait $!
	install_log="/home/${start}/Desktop/$install_log"
	wait $!
	
	echo "Updating repository lists..."
	echo 'deb http://old-releases.ubuntu.com/ubuntu/ karmic main restricted universe multiverse' >> /etc/apt/sources.list
	echo 'deb http://old-releases.ubuntu.com/ubuntu/ karmic-updates main restricted universe multiverse' >> /etc/apt/sources.list
	echo 'deb http://old-releases.ubuntu.com/ubuntu/ karmic-security main restricted universe multiverse' >> /etc/apt/sources.list
	apt-get update
	wait $!
	echo "Beginning CARBLog install for ${start}.-- "`date`>>$install_log
	######################################################################
	#
	# CONFIGURING REMOTE ACCESS
	#
	####################################################################
	echo "Remote access is being configured --experimental";
	echo "In the next window, please check or verify that ${start}"
	echo "is set to login automatically."
		gdmsetup 2>/dev/null
	wait $!
	`cat /etc/gdm/custom.conf`"--" >>$install_log
	clear
	echo "OK... Enabling remote access by default" 
	gconftool-2 --set --type=bool /desktop/gnome/remote_access/enabled true
	wait !$
	
		if (gconftool-2 -R /desktop/gnome/remote_access | grep 'enabled = true'); then
		{
			echo "Remote Access set to on"
			echo "Result Succesful --" >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'enabled ='`" -- "`date` >> $install_log
		}
		else
		{
			echo "Failed to set remote access to on"
			echo "Result Un-succesful --" >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'enabled ='`" -- "`date` >> $install_log
					
		}
		fi 
	clear
	echo "Setting your VNC password to ${start}"
	if !(gconftool-2 --type string --set /desktop/gnome/remote_access/vnc_password `echo -n ${start} | base64`); then
		{	
			echo "Result Un-succesful -- "`date` >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'vnc_passw ='`" -- "`date` >> $install_log	
		}
	else
		{
			echo "Result Succesful -- "`date` >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'vnc_passw ='`" -- "`date` >> $install_log
		}
	fi
	echo "Setting your Authentication Method..."
	if !(gconftool-2 --set --type=list --list-type=string /desktop/gnome/remote_access/authentication_methods [vnc]) then
		{	
			echo "Result Un-succesful -- "`date` >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'authentication'`" -- "`date` >> $install_log	
		}
	else
		{
			echo "Result Succesful -- "`date` >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'authentication'`" -- "`date` >> $install_log
		}
	fi
	echo "Setting your vnc to not prompt the local box to allow access..."
	if !(gconftool-2 --set --type=bool /desktop/gnome/remote_access/prompt_enabled false) then
		{	
			echo "Result Un-succesful -- "`date` >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'prompt_enabled'`" -- "`date` >> $install_log	
		}
	else
		{
			echo "Result Succesful -- "`date` >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'prompt_enabled'`" -- "`date` >> $install_log
		}
	fi
	echo "Setting your vnc to disable broadcast of the background image..."
	if !(gconftool-2 --set --type=bool /desktop/gnome/remote_access/disable_background true) then
		{	
			echo "Result Un-succesful -- "`date` >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'disable_background'`" -- "`date` >> $install_log	
		}
	else
		{
			echo "Result Succesful -- "`date` >>$install_log
			echo `gconftool-2 -R /desktop/gnome/remote_access | grep 'disable_background'`" -- "`date` >> $install_log
		}
	fi
	
	
	#clear
	#####################################################################
	#
	#SUBVERSION INSTALLATION FROM SOURCE
	#####################################################################

	if !(which svn); then
	{
		echo "No Subversion detected...installing -- "`date`>>$install_log
		echo "Installing subversion"
		apt-get install subversion
		if !(which svn); then
			{
			 	echo "Subversion installation failed, exiting";
		 	 	echo "Subversion install failed -- "`date`>>$install_log;
		 	 	exit;
			}
			else
			{
				echo "Subversion installed";
				echo "Subversion install succeded -- "`date`>>$install_log;
			}
		fi
	}
	fi
	#####################################################################
	#
	#CARBLOGGER INSTALL FROM SOURCE
	#####################################################################
	echo "Would you like to get a fresh install of the CARBLogger files? [Y/n]";
	read response;
	if [[ $response == "Y" || $response == "y" || $response == "yes" ]]; then
	{ 
		echo "User chose to install CARBLog from repository -- "`date`>>$install_log;		
		svn export https://carblogger.svn.sourceforge.net/svnroot/carblogger "/home/${start}/CARBLog" --force
		wait $!
		chmod -R 777 "/home/${start}/CARBLog"
		echo "Permissions changed -- "`date`>>$install_log;
		chown -R ${start}:${start} "/home/${start}/CARBLog"
		echo "Owner changed -- "`date`>>$install_log;
	}
	else
	{
		echo "Skipping CARBLog Install"
		echo "CARBLog was not downloaded-- "`date`>>$install_log;
		echo "Checking existing install-- "`date`>>$install_log;
		echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--- "`date`>>$install_log;		
		if !( ls -lah -R "/home/${start}/CARBLog" | grep -v ${start} >>$install_log ); then
		{
			echo "Not seeing any CARBLog here?-- "`date`>>$install_log;
		}
		fi
		echo "Checking config files... "`date`>>$install_log;
		cat "/home/${start}/CARBLog/Config/config">>$install_log;
		cat /home/${start}/CARBLog/resources/*_drv/.usr[1-9]>>$install_log;
		echo "Checking for temporary files... "`date`>>$install_log;
		ls -l -R /home/${start}/CARBLog/*~ >>$install_log;
		echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--- "`date`>>$install_log;
	}
	fi
	clear
	#############################################################################################################
	#
	#MAKE CARBLOGGER AUTOSTART
	#
	#############################################################################################################
	clear
	echo "Confirming CARBLogger will autostart..."
	rm /home/${start}/.config/autostart
	mkdir /home/${start}/.config/autostart
	chmod 755 /home/${start}/.config/autostart
	if !(cp "/home/${start}/CARBLog/sys_resources/carblogger.desktop" "/home/${start}/.config/autostart/"); then
		{
			echo "Problem copying over the autostart "`date`>>$install_log;
		}
	else
		{
			echo "Copying over the autostart successful "`date`>>$install_log;
		}
	fi 
	###################################################################
	#
	#MAKE ENABLE THE FIREWALL AND PREVENT BRUTEFORCE ON VNC
	#
	##################################################################
	if !(which ufw); then
		{
			echo "UFW not here...installing"
			apt-get install gufw
			wait $!
			apt-get install ufw
			wait $!
		}
	else
		{
			echo "ufw here"
		}
	fi
			
	sudo ufw enable
	ufw allow in 5900/tcp
	
	#echo '#!/bin/bash'>/etc/network/if-up.d/bfa_protection
	#echo '[ "${METHOD}" != loopback ] || exit 0'>>/etc/network/if-up.d/bfa_protection
	#echo '/sbin/iptables -A INPUT -i eth0 -p tcp --dport 5900 -m state --state NEW -m recent--set --name VNC'>>/etc/network/if-up.d/bfa_protection
	#echo '/sbin/iptables -A INPUT -i eth0 -p tcp --dport 5900 -m state --state NEW -m recent --update --seconds 60 --hitcount 10 --rttl --name VNC -j DROP'>>/etc/network/if-up.d/bfa_protection
	#echo '/sbin/iptables -A INPUT -i ppp0 -p tcp --dport 5900 -m state --state NEW -m recent--set --name VNC'>>/etc/network/if-up.d/bfa_protection
	#echo '/sbin/iptables -A INPUT -i ppp0 -p tcp --dport 5900 -m state --state NEW -m recent --update --seconds 60 --hitcount 10 --rttl --name VNC -j DROP'>>/etc/network/if-up.d/bfa_protection
	#echo '#!/bin/bash'>/etc/network/if-down.d/bfa_protection
	#echo '[ "${METHOD}" != loopback ] || exit 0'>>/etc/network/if-down.d/bfa_protection
	#echo '/sbin/iptables -D INPUT -i eth0 -p tcp --dport 5900 -m state --state NEW -m recent --set --name VNC' >>/etc/network/if-down.d/bfa_protection
	#echo '/sbin/iptables -D INPUT -i eth0 -p tcp --dport 5900 -m state --state NEW -m recent --update --seconds 60 --hitcount 10 --rttl --name VNC -j DROP'>>/etc/network/if-down.d/bfa_protection
	#echo '/sbin/iptables -D INPUT -i ppp0 -p tcp --dport 5900 -m state --state NEW -m recent --set --name VNC' >>/etc/network/if-down.d/bfa_protection
	#echo '/sbin/iptables -D INPUT -i ppp0 -p tcp --dport 5900 -m state --state NEW -m recent --update --seconds 60 --hitcount 10 --rttl --name VNC -j DROP'>>/etc/network/if-down.d/bfa_protection
	
	#####################################################################
	#
	#ESCALATE PRIVS OF MAIN ACCOUNT
	#####################################################################
	
	echo "WARNING: THIS ACCOUNT IS BEING ESCALATED..."
	if !(echo "${start} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers); then
		{
			echo "Problems granting No Passwd Sudo Access-- "`date`>>$install_log;
		}
	else
		{
			echo "No Password/Sudo Access granted-- "`date`>>$install_log;	
		}
	fi
	
	if !(sudo sed '$!N; /^\(.*\)\n\1$/!P; D' /etc/sudoers > /etc/sudoers.tmp); then
		{
			echo "Unable to clean up the sudoers file and create the temp file!?!-- "`date`>>$install_log;
		}
	else
		{
			echo "Sudoers file cleaned-- "`date`>>$install_log;
		}
	fi
	if !(mv /etc/sudoers.tmp /etc/sudoers); then
		{
				echo "Unable to replace the sudoers file!-- "`date`>>$install_log;
		}
	else
		{
			echo "Sudoers file replaced!-- "`date`>>$install_log;
		}
	fi
	
	if !(chmod 0440 /etc/sudoers); then
		{
			echo "OUCH! Unable to change sudoers perms to 0440 which FUBARS debian sudo!-- "`date`>>$install_log;	
		}
	else
		{
			echo "Sudoers perms changed to 0440-- "`date`>>$install_log;			
		}
	fi

	#####################################################################
	#
	#DDCLIENT INSTALLATION
	#CONFIGURATION FOR REMOTE ACCESS AND USE
	#####################################################################
	echo "Will this computer use a dynamic IP address? If so, and if you have"
	echo "a DYN-DNS account, answer Y, otherwise we will skip this step."
	read response
	if [[ $response == "Y" || $response == "y" || $response == "yes" ]]; then
	{
		apt-get install ddclient
		wait $!
		ddclntrns=`ps -fe | grep ddclient | grep -v grep | wc -l` 
		if [[ $ddclntrns -eq 0 ]]; then
		{
			echo "It appears that dynamic dns client installation was unsuccesful."
		}
		fi 
	}
	fi
	#####################################################################
	#
	#Install sjinn
	#
	#####################################################################
	if !(which sjinn); then
	{
	echo "Downloading sjinn";
	
		if !(wget http://sourceforge.net/projects/sjinn/files/sjinn-1.01/Sjinn%201.01/sjinn-1.01.tar.gz); then
		{
			echo "Sorry, we were unable to obtain a copy of sjinn from SourceForge-- "`date`>>$install_log;

		}
			else
		{
			echo "A new copy of sjinn was obtained from SourceForge-- "`date`>>$install_log;		
		}
		fi
	
	echo "See if we need build essential here..."
	if !(apt-get install build-essential); then
		{
			echo "May have problems with sjinn install because BE not installed..-- "`date`>>$install_log;		
		}
		else
		{
			echo "Build essential installed-- "`date`>>$install_log;
		}
	fi
	wait $!
	#get serial device drivers from EVERYWHERE
	serialdevs='apt-get install libboost-serialization1.42.0 inputattach python-serial ser2net libserial0 cereal libhdf5-serial-1.8.4 libhdf5-serial-dev libserial-dev screen setserial'
	if !(serialdevs); then
		{
			echo "Installing serial device drivers failed (may still function)-- "`date`>>$install_log;
		}
	else
		{
			echo "Installing serial device drivers--success-- "`date`>>$install_log;
		}
	fi
	wait $!
	echo "Installing gtkTerm-- "`date`>>$install_log;
	
	if !(apt-get install gtkterm); then
		{
		echo "gtkTerm install failed-- "`date`>>$install_log;
		}
	else
		{
		echo "gtkTerm install success-- "`date`>>$install_log;
		}
	fi
	wait $!
	if !(tar -xvvf sjinn-1.01.tar.gz); then
		{
		echo "sjinn extract failed-- "`date`>>$install_log;
		}
	else
		{
		echo "sjinn extract success-- "`date`>>$install_log;
		}
	fi
	
	if !(chown -R ${start}:${start} sjinn); then
		{
		echo "sjinn ownership change failed-- "`date`>>$install_log; 	
		}
	else
		{
		echo "sjinn ownership change success-- "`date`>>$install_log;
		}
	fi
	
	
	if !(chmod -R 777 sjinn); then
		{
			echo "sjinn mode change failed-- "`date`>>$install_log;
		}
	else
		{
			echo "sjinn mode change success-- "`date`>>$install_log;
		}
	fi
	wait $!
	if !(sed -i 's/#define MAXREADCHARS 1024/#define MAXREADCHARS 3048/g' sjinn/rs232.h); then
		{
			echo "sjinn header modification failed-- "`date`>>$install_log;
		}
	else
		{
			echo "sjinn header modification change success-- "`date`>>$install_log;
		}
	fi
	wait $!
	if !(sed -i 's/ac_default_prefix=\/usr\/local/ac_default_prefix=\/usr/g' sjinn/configure); then
		{
			echo "configure localization customization failed ---- "`date`>>$install_log;
		}
	else
		{
			echo "configure localization customization change success ---- "`date`>>$install_log;
		}
	fi
	wait $!
	cd sjinn
	if !(autoconf 'configure.in'); then
		{
			echo "autoconf failed ---- "`date`>>$install_log;
		}
	else
		{
			echo "autoconf success ---- "`date`>>$install_log;
		}
	fi
	wait $!
	if !(./configure); then
		{
			echo "sjinn configure failed-- "`date`>>$install_log;
		}
	else
		{
			echo "sjinn configure success-- "`date`>>$install_log;
		}
	fi
	if !(make); then
		{
			echo "sjinn make failed-- "`date`>>$install_log;
		}
	else
		{
			echo "sjinn make success-- "`date`>>$install_log;
		}
	fi
	if !(make install); then
		{
			echo "sjinn make install failed-- "`date`>>$install_log;
		}
	else
		{
			echo "sjinn make install success-- "`date`>>$install_log;
		}
	fi
	wait $!



	}
	fi
	
	
	
	#####################################################################
	#
	#Install mysql 
	#
	#####################################################################
	
	if !( which mysql ); then
	{
	echo "No mysql installed here. Would you like me to install it? (Y or N)";
	read response;
		if [ $response == Y ] || [ $response == y ] ; then
			{
			cd "/home/${start}/CARBLog"
			wait $!
			bash "sys_resources/mysql.install";
			wait $!
			cd "/home/${start}"
			wait $!;
			}
		else
			{
			echo "Nothing left to do;"
			exit;
			}
		fi	
	}
	else
	{
	aminstalled=`mysql -u root --password=carblogger -e "DESCRIBE cl_ingests;" carblogger | wc -l`
	if [ $aminstalled -lt 3 ]; then
		{
		echo "Hmm...lights are on but no-one is home. Want me to set up a carblogger database for you?";
		read response;
		if [ $response=="Y" ]; then
			{
				echo "Installing the ingest tables";
				mysql -u root --password=carblogger < "/home/${start}/CARBLog/sys_resources/alldb_backup.sql"				
			}
		fi
		}
	fi
	}

###########################################################################################################
#
# CONFIGURE EMAIL CAPABILITIES USING MUTT - MJV
# BECAUSE OF SPAM FILTERS, WE WILL USE SMTP/GMAIL
# USER NEEDS ACCOUNT AND PASS INFO
#
#########################################################################################################
if !(which mutt); then
{

	wait $!	
	clear;
		echo "CARBLogger uses Mutt to send emails";
		echo "Installing for GMAIL.."
		apt-get install openssl mutt
		wait $!
		mkdir -p "/home/${start}/.mutt/cache/headers"
		wait $!
		mkdir "/home/${start}/.mutt/cache/bodies"
		wait $!
		#touch "/home/${start}/.mutt/certificates"
		cp /etc/ssl/certs/Equifax_Secure_CA.pem "/home/${start}/.mutt/certificates"		
		wait $!
		# A basic .muttrc for use with Gmail
		
		# Change the following six lines to match your Gmail account details
		echo "What is your email address, not including the gmail.com";
		read gmail_addr
		echo 'set imap_user = "'$gmail_addr'@gmail.com"' > /home/${start}/.muttrc
		echo "What is your password";
		read gmail_pass
		wait $!
		echo 'set imap_pass = "'$gmail_pass'"' >> /home/${start}/.muttrc
		echo 'set smtp_url = "smtp://'$gmail_addr'@smtp.gmail.com:587/"' >> /home/${start}/.muttrc 
		echo 'set smtp_pass = "'$gmail_pass'"' >> /home/${start}/.muttrc
		echo 'set from = "'$gmail_addr'@gmail.com"' >> /home/${start}/.muttrc
		echo 'set realname = "CARBDATA"' >> /home/${start}/.muttrc

	echo '# Change the following line to a different editor you prefer.
	set editor = "gedit"

	# Basic config, you can leave this as is
	set folder = "imaps://imap.gmail.com:993"
	set spoolfile = "+INBOX"
	set imap_check_subscribed
	set hostname = gmail.com
	set mail_check = 120
	set timeout = 300
	set imap_keepalive = 300
	set postponed = "+[GMail]/Drafts"
	set record = "+[GMail]/Sent Mail"
	set header_cache=~/.mutt/cache/headers
	set message_cachedir=~/.mutt/cache/bodies
	set certificate_file=~/.mutt/certificates
	set move = no
	set include
	set sort = "threads"
	set sort_aux = "reverse-last-date-received"
	set auto_tag = yes
	ignore "Authentication-Results:"
	ignore "DomainKey-Signature:"
	ignore "DKIM-Signature:"
	hdr_order Date From To Cc
	alternative_order text/plain text/html *
	auto_view text/html
	bind editor <Tab> complete-query
	bind editor ^T complete
	bind editor <space> noop 
	
	# Gmail-style keyboard shortcuts
	macro index,pager y "<enter-command>unset trash\n <delete-message>" "Gmail archive message"
	macro index,pager d "<enter-command>set trash=\"imaps://imap.googlemail.com/[GMail]/Bin\"\n <delete-message>" "Gmail delete message"
	macro index,pager gi "<change-folder>=INBOX<enter>" "Go to inbox"
	macro index,pager ga "<change-folder>=[Gmail]/All Mail<enter>" "Go to all mail"
	macro index,pager gs "<change-folder>=[Gmail]/Starred<enter>" "Go to starred messages"
	macro index,pager gd "<change-folder>=[Gmail]/Drafts<enter>" "Go to drafts"' >> /home/${start}/.muttrc
}
else
{
	echo "email is already installed";
}
fi
clear
echo "Finally, CARBLogger is going to look at your current machine";
echo "configuration. We will attempt to enable any additional serial port/cards";
echo "you have added. Press [ENTER] to continue..."
####################################################################
read
wait $!
clear
###################################################################
#
#CHECK THE PHYSICAL NUMBER OF SERIAL PORTS CONFIGURED
#AGINST THE USER SELECTED NUMBER
##################################################################
#THIS FUNCTION JUST ACCEPTS the NUMBER OF SERIAL PORTS FROM A USER
#ANY NUMBER BETWEEN 1 AND 9 WOULD BE OK.
get_serial(){
	number_serial=''
	while [ -z ${number_serial} ]; 
		do
		echo "How many TOTAL serial ports should this machine have (not including USB)?"
		read number_serial
		if [[ "${number_serial}" -gt "1" ]] && [[ "${number_serial}" -lt "10" ]]; then
        		{
			:         
        		}
		else
		        {
			echo "Invalid response. Should be a number between 1 and 9";
			number_serial=''
        		}
		fi 
		done	
	}
get_serial
wait $!
echo "User stated that their machine should have a total of ${number_serial} serial ports-- "`date`>>$install_log;
###################################################################
#
# FUNCTIONS FOR MODIFYING THE BOOT RECORD
#
###################################################################
#FUNCTION THAT COPIES THE FIRST DEFAULT BOOT OPTION
#AND CREATES ONE UNDER 40_custom with the appropriate number of serial ports
#BE SURE YOU DON'T CALL THIS FUNCTION UNLESS YOU WANT A CHANGE
mk_bt_entry()
{
	sed -n '/### BEGIN \/etc\/grub.d\/10_linux ###/,/}$/p' /boot/grub/grub.cfg | sed 's/splash$/splash 8250.nr_uarts='${number_serial}'/g' | grep -v ^\#\#\# | sed 's/-generic (\/dev/-CARBLOG (\/dev/g' >> /etc/grub.d/40_custom
	wait $!
	grub-mkconfig
	update-grub
}
#Function determines how many physical serial ports the machine can currently accept
#assuming that the correct boot option was seleceted
bts_mtchng_ser()
{
	cat /boot/grub/grub.cfg | grep 8250.nr_uarts=${number_serial}
}
#function lists existing boot records
lst_bt_rcds()
{
count=0
while read line;
do
		echo $line
done < /boot/grub/grub.cfg | grep menuentry
 
}
#function inputs ls_bt_rcds and produces the boot numbers
enum_bt_rcds()
{
	lst_bt_rcds | awk '{print FNR "\t" $0}'
}
#Function produces the number of the CARBLOG line from the boot records
cblg_bt_rcd()
{
	enum_bt_rcds | grep CARBLOG | awk '{ print $1 - 1}'
	 
}
#Function sets the default boot menu entry to be the one generated by CARBLogger
cblg_bt_stdflt()
{
new_default=`cblg_bt_rcd`
sed -i -e 's/GRUB_DEFAULT=[0-9]/GRUB_DEFAULT='${new_default}'/g' /etc/default/grub
wait $!
update-grub 	
wait $!	 
}
#Function determines the boot option which has been previously configured by CARBLogger
bts_mtchng_cbl()
{
	if [[ `cat /boot/grub/grub.cfg | grep CARBLOG | wc -l` -ge "1" ]]; then
		{
			return 0;
		}
	else
		{
			return 1;
		}
	fi
}
#Function determines if the current machine has the same number of serial ports as the desired number
crnt_mtchng_dsrd()
{
	current_devices=`ls /dev/ttyS* | wc -w`
	if [[ "${current_devices}" -eq "${number_serial}" ]]; then
		{
			return 0;
		}
	else
		{
			return 1;
		}
	fi
}
echo "DETERMINING BOOT CONFIGURATION(s)"`date`>>$install_log;
echo "crnt_mtchng_dsrd = "`crnt_mtchng_dsrd`" --"`date`>>$install_log;
echo "bts_mtchng_cbl = "`bts_mtchng_cbl`" --"`date`>>$install_log;
echo "bts_mtchng_ser = "`bts_mtchng_ser`" --"`date`>>$install_log;

if (crnt_mtchng_dsrd); then
	{
		clear;
		echo "Your device already has the correct number of serial"
		echo "ports. No further configuration will be made."
		echo "since crnt_mtchng_dsrd = 1 no further boot config "`crnt_mtchng_dsrd`" --"`date`>>$install_log; 
	}
		else
	{
		echo "CONTINUE AUTOMATION OF BOOT HERE..."
		echo "since crnt_mtchng_dsrd = 0 further boot config is required "`crnt_mtchng_dsrd`" --"`date`>>$install_log;
		if (bts_mtchng_cbl); then
			{
			
				echo "Your device already has a CARBLogger Configured Boot Record"
				echo "No further boots will be added."
				echo "We will try to match your default login to use that boot however."
				cblg_bt_stdflt
				echo "since bts_mtchng_cbl = 1 no further boot config "`bts_mtchng_cbl`" --"`date`>>$install_log;
			
				
			}
		else
			{
			clear 
			
			#BEGIN ACTUALLY MODIFYING THE MASTER BOOT RECORD TO ADD SERIAL PORTS TO ONE NEW CONFIGURATION
			echo "No previous CARBLogger BOOT configuration HERE..."
			echo "Would you like to add a master boot record which can accept ${number_serial} total serial ports (Y/n)?"
			read frthr_bt_instll
			wait $!
			case "$frthr_bt_instll"	in
				y|Y|yes|Yes|YES)
					if !(mk_bt_entry); then
						{
						echo "New boot entry failure..."
						echo "New boot entry failure  --"`date`>>$install_log;
						cat "/boot/grub/grub.cfg" >>$install_log;
					
						}
					else
						{
						echo "New boot entry succeded..."
						echo "New boot entry succeded  --"`date`>>$install_log;
						cat "/boot/grub/grub.cfg" >>$install_log;
						cblg_bt_stdflt
						}
					fi			
					;;
					
				n|N|no|No|NO)
					echo "No New boot entry chosen..."
					echo "New boot entry not made by choice  --"`date`>>$install_log;
					cat "/boot/grub/grub.cfg" >>$install_log;
				
				;;
				*) 
					echo "Not an answer...no changes being made..."
					echo "No New boot entry ...truculent user response"
					echo "truculent user response  --"`date`>>$install_log;
					cat "/boot/grub/grub.cfg" >>$install_log;
				;;
			esac			
			
			
			}
		fi
		
					
	}
fi

echo "Installation complete"
echo "rebooting..hit [Enter] to continue."
read ready
wait $!
sleep 10
reboot now
exit;
fi
}
fi 
